package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinOrderResult;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */

public class RewardOrderAdapter extends BaseAdapter {
    private Context context;
    private List<String> goodsid;
    private JSONArray info;
    private List<String> imgList;
    private List<String> goodsname;
    private List<String> goodsnum;
    private List<String> shouldprice;
    private List<String> gattr;

    public RewardOrderAdapter(Context context, RewardCoinOrderResult.RewardCoinOrder data, int position) {
        this.context = context;
        this.goodsid = data.getGoodsId();
        this.imgList = data.getImg();
        this.goodsname = data.getGoodsName();
        this.goodsnum = data.getGoodsNum();
        this.shouldprice = data.getShouldPrice();
        this.gattr = data.getAttr();
    }

    @Override
    public int getCount() {
        return goodsid.size();
    }

    @Override
    public Object getItem(int position) {
        return info;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try{
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_order_list, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvAttr = (TextView) convertView.findViewById(R.id.tvAttr);
                holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
                holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
//            if(null!=data&&!"".equals(data)){
                if(null!=imgList.get(position)&&!"".equals(imgList.get(position))){
                    Glide.with(context).load(Url.IMG_URL+imgList.get(position)).placeholder(R.mipmap.iv_eg).fitCenter().into(holder.ivImg);
                    holder.tvName.setText(goodsname.get(position));
                    holder.tvPrice.setText(shouldprice.get(position)+"元[返银币"+new BigDecimal(shouldprice.get(position)).intValue()+"银币]");
                    holder.tvNum.setText("数量：X"+goodsnum.get(position));
                    Log.i("base", "num+++"+goodsnum.get(position));
                    String attr = gattr.get(position);
                    JSONObject obj = new JSONObject(attr);
                    Iterator<String> iterator = obj.keys();
                    String str = "";
                    while (iterator.hasNext()){
                        str += "/"+obj.getString(iterator.next());
                    }
                    holder.tvAttr.setText(str);
                }
//            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder{
        private ImageView ivImg;
        private TextView tvName;
        private TextView tvAttr;
        private TextView tvPrice;
        private TextView tvNum;
    }
}
